package com.tools_s7.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.widget.LinearLayoutManager;

import com.tools.s7.delegate.base.ActivityPresenter;
import com.tools.s7.recycler.utils.LoadMoreHelper;
import com.tools_s7.TestDelegate;
import com.tools_s7.adapter.TestAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 视图模式 + recycler
 */
public class TestActivity extends ActivityPresenter<TestDelegate> {

    private LinearLayoutManager layoutManager;
    private List<String> datas;

    private LoadMoreHelper mHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        datas = new ArrayList<>();

        layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mHelper = new LoadMoreHelper.Builder<String>()
                .setSwipeRefresh(viewDelegate.swipeRefresh, Color.YELLOW, Color.BLUE, Color.GREEN)
                .setRecycler(viewDelegate.recycler, layoutManager)
                .setAdapter(new TestAdapter().setError(true).setFooter(true))
                .setLoadData(new LoadMoreHelper.LoadDataListener() {
                    @Override
                    public void onLoadData(int page) {
                        getData(page);
                    }
                }).build();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mHelper.loadFirstPage();
    }

    private int s = 0;
    public void getData(int page) {
        if (page == 1) {
            s = 0;
        }

        int e = page * 10;

        if (page == 3) {
            e = e - 3;
        }

        datas.clear();

        for (int i=s; i<e; i++) {
            datas.add("你好" + i);
        }
        s = e;
        CountDown(3);
    }

    public void CountDown(int s) {
        new CountDownTimer(s * 1000, 1000) {
            @Override
            public void onTick(long l) {

            }

            @Override
            public void onFinish() {
                mHelper.notifyAdapter(datas);
//                adapter.addData(datas);
            }
        }.start();
    }
}
